/*
 * Decodes a json string data object into objects of type ChatMsg, private
 */
package ws.websk.chatEndpoint2;

import ws.websk.chatEndpoint2.messages.Message;
import ws.websk.chatEndpoint2.messages.ChatMessage;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonReader;
import javax.websocket.DecodeException;
import javax.websocket.Decoder;
import javax.websocket.EndpointConfig;
import ws.websk.chatEndpoint2.messages.NicknameMessage;
import ws.websk.chatEndpoint2.messages.PrivateMessage;

/**
 * @author Jens-Marius Nergård Hansen
 */
public class MsgDecoder implements Decoder.Text<Message> {
    private JsonObject jsonObject;
    
    @Override
    public Message decode(String s) throws DecodeException {
        Message msg = null;
         if (willDecode(s)) { // is this check redundant? (glassfish calls willDecode() prior to calling decode())
            switch (jsonObject.getString("type")) {
                case "chat":
                    return new ChatMessage(  jsonObject.getString("nickname"), 
                                             jsonObject.getString("message"));
                case "nickname":
                    return new NicknameMessage(jsonObject.getString("nickname"));
                case "private":
                    return new PrivateMessage(  jsonObject.getString("sender"), 
                                                jsonObject.getString("receiver"), 
                                                jsonObject.getString("message"));
            }
         } else {
             System.out.println("Cant decode!" );
             throw new DecodeException(s, "[Message] Can't decode.");
         }
         return msg;
    }

    @Override
    public boolean willDecode(String s) {
        InputStream inputStream = new ByteArrayInputStream(s.getBytes());
        JsonReader jsonReader = Json.createReader(inputStream);
        jsonObject = jsonReader.readObject();

        switch (jsonObject.getString("type")){
            case "chat":     //send a broadcast message
                if (jsonObject.containsKey("message"))
                    return true;
            case "nickname": //set nickname message
                if(jsonObject.containsKey("nickname"))
                    return true;
            case "private": //send private message
                if(jsonObject.containsKey("receiver") && 
                   jsonObject.containsKey("message") &&
                   jsonObject.containsKey("sender"))
                    return true;
            default:
                return false;
        }
    }

    @Override
    public void init(EndpointConfig config) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void destroy() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}